package com.sqsf.test;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "CServlet", value = "/c")
public class CServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext servletContext = request.getServletContext();
        //spring-web提供的监听器工具类也会将spring容器对象存储在servletContext域中
        //但是我们不知道name是什么  而且即时知道 取出的数据还需要进行强制转换后才能使用
        //spring-web提供了另一个工具类WebApplicationContextUtils
        //调用其静态方法getWebApplicationContext传入servletContext域对象获取spring容器对象
        WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        User bean = applicationContext.getBean(User.class);
        System.out.println(bean);
    }

}
